package bingosoft.metro.datasync.uuap.service;

import bingosoft.components.base.helper.ObjectHelper;
import bingosoft.components.base.service.Result;
import bingosoft.components.base.service.ResultEntity;
import bingosoft.metro.datasync.base.service.SyncService;
import bingosoft.metro.datasync.uuap.UuapClientFactory;
import bingosoft.metro.datasync.uuap.client.form.OrgDataForm;
import bingosoft.metro.datasync.uuap.client.param.RequestParam;
import bingosoft.metro.datasync.uuap.client.service.DataClientService;
import bingosoft.metro.datasync.uuap.model.SyncOrgModel;
import leap.core.AppContext;
import leap.orm.model.Model;
import org.dom4j.DocumentException;

import java.util.*;

/**
 * @author Feily
 * @功能描述 组织增量同步数据服务类
 * @创建时间 2018-07-31
 */
public class OrgSyncService extends SyncService {
	
	/**
	 * 组织角色关系类
	 */
	//protected List<SyncOrgRoleRefModel> orgRoleRefs = new ArrayList<SyncOrgRoleRefModel>();
	/**
	 * 删除组织角色中间表SQL
	 */
	//protected String deleteOrgRoleSyncDataSql;
	/**
	 * 删除组织角色目标表SQL
	 */
	//protected String deleteOrgRoleTargetDataSql;
	/**
	 * 保存组织角色目标表SQL
	 */
	//protected String saveOrgRoleTargetDataSql;
	
	/**
	 * 构建函数
	 */
	public OrgSyncService() throws DocumentException {
		this.setLogClass(this.getClass());

		this.tenantCode = AppContext.config().getProperty("uuap.tenantCode");
		this.syncType = "org";
		this.syncConfigDocPath = "/conf/config/sync-org-config.xml";

		if(useCustomSql){
			this.getLastSyncTimeSql = this.getSqlContent("uuap-org-getLastSyncTime");
			this.saveTargetDataSql = this.getSqlContent("uuap-org-saveTargetData");
			this.updateTargetDataSql = this.getSqlContent("uuap-org-updateTargetData");
			this.deleteTargetDataSql = this.getSqlContent("uuap-org-deleteTargetData");
		}else{
			this.getLastSyncTimeSql = this.buildGetLastUpdateTimeSql();
			this.saveTargetDataSql = this.buildSaveTargetDataSql();
			this.updateTargetDataSql = this.buildUpdateTargetDataSql();
			this.deleteTargetDataSql = this.buildDeleteTargetDataSql();
		}

		//删除组织同步中间表数据SQL
		this.deleteSyncDataSql = this.getSqlContent("uuap-org-deleteSyncData");
		
		/*this.deleteOrgRoleSyncDataSql = this.getSqlContent("uuap-orgRole-deleteSyncData");
		this.deleteOrgRoleTargetDataSql = this.getSqlContent("uuap-orgRole-deleteTargetData");
		this.saveOrgRoleTargetDataSql = this.getSqlContent("uuap-orgRole-saveTargetData");*/
	}
	
	@Override
	public Result deleteSyncData() {
		this.logMethodCalled("deleteSyncData");

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("tenantCode", this.tenantCode);

		// 删除组织角色同步表
		//getDao().executeUpdate(this.deleteOrgRoleSyncDataSql, params);
		
		// 删除组织同步表
		getDao().executeUpdate(this.deleteSyncDataSql, params);
		
		return new Result();
	}

	@Override
	public Result saveSyncData(List<Model> syncData) {
		this.logMethodCalled("saveSyncData");
		
		// 保存组织
		getDao().batchInsert(syncData);
		
		// 保存组织角色关系
		//getDao().batchInsert(this.orgRoleRefs);
		
		return new Result();
	}

	@Override
	public Result syncTargetData() {
		this.logMethodCalled("syncTargetData");

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("tenantCode", this.tenantCode);

		// 删除目标表中的已删除数据
		if(this.physicallyDelete){
			getDao().executeUpdate(this.deleteTargetDataSql, params);
		}

		// 修改目标组织数据
		getDao().executeUpdate(this.updateTargetDataSql, params);
		
		// 保存目标组织数据
		getDao().executeUpdate(this.saveTargetDataSql, params);
		
		// 删除目标组织角色关系数据
		//getDao().executeUpdate(this.deleteOrgRoleTargetDataSql, params);
		
		// 保存目标组织角色关系数据
		//getDao().executeUpdate(this.saveOrgRoleTargetDataSql, params);
		
		return new Result();
	}

	@Override
	public List<Model> getSyncDataList(Date lastSyncTime) {
		this.logMethodCalled("getSyncDataList");
		this.logParamValue("lastSyncTime", lastSyncTime);
		
		List<Model> list = new ArrayList<Model>();
		
		DataClientService service  = UuapClientFactory.getDataClientService();
		
		RequestParam requestParam = new RequestParam();
		requestParam.setUpdateBeginDate(lastSyncTime);
		requestParam.setUpdateEndDate(new Date());
		
		ResultEntity<List<OrgDataForm>> resultEntity = service.queryOrgs(requestParam);
		if(!resultEntity.isSuccess()){
			throw new RuntimeException(resultEntity.getMessage());
		}
		
		// 构建数据
		List<OrgDataForm> orgs = resultEntity.getBusinessObject();
		for (OrgDataForm orgDataForm : orgs) {
			
			// 构建组织
			SyncOrgModel org = new SyncOrgModel();
			orgDataForm.copyTo(org);
			org.setTenantCode(this.tenantCode);
			
			org.setSyncId(ObjectHelper.getUUID());
			org.setSyncTime(new Date());
			org.setIsSync(true);
			list.add(org);
			
			// 构建组织角色关系
			/*for (OrgRoleDataForm orgRoleDataForm : orgDataForm.getRoles()) {
				SyncOrgRoleRefModel ref = new SyncOrgRoleRefModel();
				ref.setOrgRoleId(orgRoleDataForm.getOrgRoleId());
				ref.setRoleId(orgRoleDataForm.getRoleId());
				
				ref.setOrgId(orgDataForm.getOrgId());
				ref.setCreateTime(orgDataForm.getCreateTime());
				ref.setCreateUserId(orgDataForm.getCreateUserId());
				ref.setUpdateTime(orgDataForm.getUpdateTime());
				ref.setUpdateUserId(orgDataForm.getUpdateUserId());
				ref.setTenantCode(this.tenantCode);
				
				ref.setSyncId(ObjectHelper.getUUID());
				ref.setSyncTime(new Date());
				ref.setIsSync(org.getIsSync());
				
				this.orgRoleRefs.add(ref);
			}*/
		}
		return list;
	}

	/*public List<SyncOrgRoleRefModel> getOrgRoleRefs() {
		return orgRoleRefs;
	}

	public void setOrgRoleRefs(List<SyncOrgRoleRefModel> orgRoleRefs) {
		this.orgRoleRefs = orgRoleRefs;
	}*/

}
